<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Guestbook_model extends Model
{
	function Guestbook_model()
	{
		parent::Model();
	}
	function get_records($per_page=10)
	{
		$this->load->library('pagination');
		parse_str($_SERVER['QUERY_STRING'],$get);
		if(isset($get['s_date'])&&$get['s_date']!='')
		{
			$this->db->where('create_date >=',strtotime($get['s_date']));
		}
		if(isset($get['e_date'])&&$get['e_date']!='')
		{
			$this->db->where('create_date <=',strtotime($get['e_date']));
		}
		if(isset($get['is_pass'])&&$get['is_pass']!='')
		{
			$this->db->where('is_pass',$get['is_pass']);
		}	
		if(isset($get['s_keyword'])&&$get['s_keyword']!='')
		{
			$this->db->like('email',$get['s_keyword']);
			$this->db->or_like('user_name',$get['s_keyword']);
			$this->db->or_like('tel',$get['s_keyword']);
		}
		$total_rows = $this->db->count_all_results('guestbook');
		
		if(isset($get['s_date'])&&$get['s_date']!='')
		{
			$this->db->where('create_date >=',strtotime($get['s_date']));
		}
		if(isset($get['e_date'])&&$get['e_date']!='')
		{
			$this->db->where('create_date <=',strtotime($get['e_date']));
		}
		if(isset($get['is_pass'])&&$get['is_pass']!='')
		{
			$this->db->where('is_pass',$get['is_pass']);
		}	
		if(isset($get['s_keyword'])&&$get['s_keyword']!='')
		{
			$this->db->like('email',$get['s_keyword']);
			$this->db->or_like('user_name',$get['s_keyword']);
			$this->db->or_like('tel',$get['s_keyword']);
		}
		$this->db->order_by('id','desc');
		$offset = $this->uri->segment(4, 0);
		if($total_rows==$offset||$total_rows<$offset)
		{
			$offset=0;
		}
		$this->pagination->initialize(
			array(
					'base_url'		 => CTL_FOLDER.'guestbook/index',
					'total_rows'	 => $total_rows,
					'per_page'		 => $per_page,
					'cur_page'       => $offset
			)
		);
		$data=array(
			'query'    => $this->db->get("guestbook", $per_page, $offset),
			'paginate' => $this->pagination->create_links()
		);
		return $data;
	}
	
	function get_record($id)
	{
		$query=$this->db->get_where("guestbook",array('id'=>$id));
		if($query->num_rows()>0)
		{
			return $query->row_array();
		}
	}
	
	function save_record()
	{
		$rd_id=$this->input->post("rd_id");
		$data=array(
			'reply' => $this->input->post("reply"),
			'reply_date' => time()
		);
		$this->db->update("guestbook",$data,array('id'=>$rd_id));
		return TRUE;
	}
	
	function del_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->delete("guestbook");
		if($this->db->affected_rows())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function pass()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$data=array(
			'is_pass' => 1
		);
		$this->db->where_in("id",$rd_id);
		$this->db->update('guestbook',$data);
		return TRUE;
	}
	
	function get_is_pass_num()
	{
		$this->db->where('is_pass',1);
		$this->db->from('guestbook');
		$data=array(
			'pass_num' => $this->db->count_all_results()
		);
		$this->db->where('is_pass',0);
		$this->db->from('guestbook');
		$data['not_pass_num']=$this->db->count_all_results();
		return $data;
	}
	
	function ajax_toggle()
	{
		$rd_id=$this->input->post('rd_id');
		$query=$this->db->get_where("guestbook",array('id'=>$rd_id));
		$rs=$query->row_array();
		$data=$rs['is_pass']?0:1;
		$this->db->update('guestbook',array('is_pass'=>$data),array('id'=>$rd_id));
		return TRUE;
	}
}
?>